<template>
  <div>
    <!-- 增加 -->
    <section-add-form v-if="showAddModal" ref="sectionAddForm" :typeOptions="typeOptions" @ok="getList" @close="showAddModal = false" :regionId="regionId" />
    <!-- 编辑 -->
    <section-edit-form v-if="showEditModal" ref="sectionEditForm" :typeOptions="typeOptions" @ok="getList" @close="showEditModal = false" :regionId="regionId" />
    <a-card :bordered="false">
      <div class="table-operations">
        <div class="table_title">版块管理</div>
        <a-button type="primary" @click="handleAdd" v-hasPermi="['platform:region:edit']"> <a-icon type="plus" />新增 </a-button>
        <a-button type="danger" v-if="!multiple" :disabled="multiple" @click="handleDelete" v-hasPermi="['platform:section:remove']"> <a-icon type="delete" />删除 </a-button>
        <a-tooltip title="刷新">
          <a-icon @click="getList" class="action" :type="loading ? 'loading' : 'reload'" />
        </a-tooltip>
      </div>
      <a-table
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }"
        tableKey="base-section-index-table"
        @change="handleTableChange"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="sectionList"
        :loading="loading"
        :format-conditions="true"
      >
        <span slot="status" slot-scope="text, record">
          <a-badge :status="record.status == '0' ? 'processing' : 'error'" :text="statusFormat(record)" />
        </span>
        <span slot="createTime" slot-scope="text, record">
          {{ parseTime(record.createTime) }}
        </span>
        <span slot="type" slot-scope="text, record">
          {{ typeFormat(record) }}
        </span>
        <span v-if="record.type === '1'" slot="operation" slot-scope="text, record">
          <a @click="handleUpdate(record)" v-hasPermi="['platform:region:edit']"> 修改 </a>
          <a-divider type="vertical" v-hasPermi="['platform:region:edit']" />
          <a @click="handleDelete(record)" v-hasPermi="['platform:region:edit']"> 删除 </a>
        </span>
      </a-table>
    </a-card>
  </div>
</template>
<script>
import { listSection, delSection, exportSection, getInitData } from '@/api/platform/section'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import SectionAddForm from '@/views/platform/section/modules/SectionAddForm'
import SectionEditForm from '@/views/platform/section/modules/SectionEditForm'
export default {
  name: 'Section',
  components: {
    AdvanceTable,
    SectionAddForm,
    SectionEditForm
  },
  props: {
    regionId: {
      type: String,
      required: false
    }
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 版块管理表格数据
      sectionList: [],
      // 类型字典
      typeOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        regionId: this.regionId
      },
      //显示标志
      visibleFlag: true,
      columns: [
        {
          title: '版块名称',
          dataIndex: 'name',
          ellipsis: true,
          align: 'center',
          width: '20%'
        },

        {
          title: '版块编码',
          dataIndex: 'code',
          ellipsis: true,
          align: 'center',
          width: '20%'
        },
        {
          title: '类型',
          dataIndex: 'type',
          ellipsis: true,
          scopedSlots: { customRender: 'type' },
          align: 'center',
          width: '20%'
        },
        {
          title: '版块排序',
          dataIndex: 'sort',
          ellipsis: true,
          align: 'center',
          width: '20%'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '20%',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created() {
    this.initial()
    this.getList()
    getInitData('section_type').then(response => {
      this.typeOptions = response.data.section_type
    })
  },
  methods: {
    initial() {
      if (this.regionId) {
        this.visibleFlag = false
      }
    },
    /** 查询版块管理列表 */
    getList() {
      this.loading = true
      listSection(this.queryParam).then(response => {
        this.sectionList = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    // 类型字典翻译
    typeFormat(row) {
      if (row.type) {
        return this.selectDictLabel(this.typeOptions, row.type)
      } else {
        return ''
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        type: undefined,

        name: undefined
      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleAdd() {
      this.showAddModal = true
      this.$nextTick(() => this.$refs.sectionAddForm.handleAdd())
    },
    handleUpdate(record, ids) {
      this.showEditModal = true
      this.$nextTick(() => this.$refs.sectionEditForm.handleUpdate(record, ids))
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this
      const sectionIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk() {
          return delSection(sectionIds).then(() => {
            that.onSelectChange([], [])
            that.getList()
            that.$message.success('删除成功', 3)
          })
        },
        onCancel() {}
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk() {
          return exportSection(that.queryParam).then(response => {
            that.download(response.msg)
            that.$message.success('导出成功', 3)
          })
        },
        onCancel() {}
      })
    },
    handleTableChange(pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    }
  }
}
</script>
